var iscreate=0;//检测是否生成图片完成
function getNowTime() {
	var d = new Date()
	var vYear = d.getFullYear()
	var vMon = d.getMonth() + 1
	var vDay = d.getDate()
	var h = d.getHours();
	var m = d.getMinutes();
	var se = d.getSeconds();
	s = vYear + (vMon < 10 ? "0" + vMon : vMon)+ (vDay < 10 ? "0" + vDay : vDay) + (h < 10 ? "0" + h : h)+ (m < 10 ? "0" + m : m) + (se < 10 ? "0" + se : se);
	return s
}
// 医院级别的选择
$('.form-level  label').on('click', function() {
	$('.form-level  label').siblings("label").removeClass('select');
	$(this).addClass('select');
	$('.form-level input').removeAttr('checked');
	var id = $(this).attr('for');
	$('#' + id).attr('checked', 'checked');
});
$("#"+tabId+" #generateBtn").on("click",function() {
	var isBlank = false;
	$.each($('[required="required"]'), function(i, n) {
		if ($(n).val() == '') {
			setTimeout(function() {
				$(n).removeClass('ue-shake');
			}, 900);
			$(n).addClass('ue-shake');
			isBlank = true;
		}
	});
	if (isBlank)
		return;
	$("#"+tabId+" #generateBtn").attr('disabled', 'disabled').find('i').addClass('fa-spin');
	$('#exportResult .panel-footer').html('<i class="fa fa-cog fa-spin fa-4x fa-fw margin-bottom text-success"></i>');
	var dateFrom = $('#'+tabId+' #sdate').val();
	var dateTo = $('#'+tabId+' #edate').val();
	var benchmarkYear = $('#'+tabId+' #benchmarkYear').val();
	var hospName=$('#'+tabId+' #hospName').val();
	var hospBuildYear=$('#'+tabId+' #hospBuildYear').val();
	var hospLevel= $('input[name="hospLevel"]:checked ').val();
	var hospBedNum=$('#'+tabId+' #hospBedNum').val();
	var hospRealBedNum=$('#'+tabId+' #hospRealBedNum').val();
	var hospCaseNum=$('#'+tabId+' #hospCaseNum').val();
	var hospDocNum=$('#'+tabId+' #hospDocNum').val();
	var hospBedDaysNum=$('#'+tabId+' #hospBedDaysNum').val();
	var mdcCode=$('#'+tabId+' #mdcCode').val();
	var isAbno= $('input[name="isAbno"]:checked ').val();
	var dephospname=$('#'+tabId+' #dephospname').val();
	var dephospcode=$('#'+tabId+' #dephospcode').val();
	
	// 导出基础数据和表格信息
	console_wt('获取分析报告基础数据...');
		$.ajax({type : "post",
				url : contextPath+ "/WordReport/findReportInfo.dhc",
				data : {
					"dateFrom":dateFrom ,
					 "dateTo": dateTo ,
					"benchmarkYear": benchmarkYear,
					"hospName":hospName,
					"hospBuildYear":hospBuildYear,
					"hospLevel":hospLevel,
					"hospBedNum":hospBedNum,
					"hospRealBedNum":hospRealBedNum,
					"hospCaseNum":hospCaseNum,
					"hospDocNum":hospDocNum,
					"hospBedDaysNum":hospBedDaysNum,
					"mdcCode":mdcCode,
					"isAbno":isAbno,
					"dephospname":dephospname,
					"dephospcode":dephospcode
				},
				/*data:{
				"a":12345,
				"b":8987
				},*/
				success : function(result) {
					if (result.success) {
						if (result.data.newdrgs == undefined|| result.data.newdrgs != 1) {
							var data = result.data;
							createPicture(data);
							$("#t1").show();
							var timer=setInterval(function(){
							    if (iscreate=='1') {
							     clearInterval(timer);
								$.ajax({   
									type : "post",
									url : contextPath+ "/WordReport/createWordReport.dhc",
									data : {},
									success : function(result) {
										if(result.success){
											var patch=result.data.patch;
											var hospname=result.data.hospname;
											var exportBtnHtml = '<div class="btn-group" role="group">';
											exportBtnHtml += '<a type="button" download="'+ hospname+ '住院病例DRG分析报告.doc" class="btn btn-default btn-lg" href="'+ patch+ '"><i class=" icon-save"></i>&nbsp;下载分析报告</a>';														
											exportBtnHtml += '</div>';
											$("#"+tabId+" #generateBtn").removeAttr('disabled','disabled').find('i').removeClass('fa-spin');
											$('#exportResult .panel-footer').html(exportBtnHtml);	
											drgUI.success('分析报告生成成功!');
										}else{
											var exportBtnHtml =result.errorMessage;
											$("#"+tabId+" #generateBtn").removeAttr('disabled','disabled').find('i').removeClass('fa-spin');
											$('#exportResult .panel-footer').html(exportBtnHtml);
											drgUI.error( result.errorMessage);
										}
										
									}
								});
							} else {
								$("#"+tabId+" #generateBtn").removeAttr('disabled', 'disabled').find('i').removeClass('fa-spin');
								$('#exportResult .panel-footer').html('<p class="text-danger">'+ data.errorMessage+ '</p>');
							}
							},1000)
						} else {
							var exportBtnHtml = '<div class="btn-group" role="group">';
							   exportBtnHtml +='<table class="newGroup" style="line-height:26px">';
							   exportBtnHtml +='<caption style="color:red">新增病组：</caption>';
							for(var i=0;i<result.data.drgsList.length;i+=2){
								if(i == result.data.drgsList.length-1){
									exportBtnHtml += '<tr><td>'+ result.data.drgsList[i][0]+ '</td><td>----</td><td>'+result.data.drgsList[i][1]+'例</td></tr>';
								}else{
									exportBtnHtml += '<tr><td>'+ result.data.drgsList[i][0]+ '</td><td>----</td><td>'+result.data.drgsList[i][1]+'例</td><td>&nbsp;</td><td>'+ result.data.drgsList[i+1][0]+ '</td><td>----</td><td>'+result.data.drgsList[i+1][1]+'例</td></tr>';
								}
							}
							exportBtnHtml += '</table>';
							exportBtnHtml += '</div>';
							$("#"+tabId+" #generateBtn").removeAttr('disabled', 'disabled').find('i').removeClass('fa-spin');
							$('#exportResult .panel-footer').html(exportBtnHtml);
							drgUI.error('本院含有新增病组，请到标杆测算页面测算标杆并保存');
						}
					} else {
						if (result.errorMessage != undefined) {
							var exportBtnHtml = '<div class="btn-group" role="group">';
							exportBtnHtml += '<span style="color:red;font-size:10px;">'+ result.errorMessage+ '</span>';
							exportBtnHtml += '</div>';
							$("#"+tabId+" #generateBtn").removeAttr('disabled', 'disabled').find('i').removeClass('fa-spin');
							$('#exportResult .panel-footer').html(exportBtnHtml);
				           drgUI.error(result.errorMessage);
						} else {
							var exportBtnHtml = '<div class="btn-group" role="group">';
							exportBtnHtml += '<span style="color:red;font-size:10px;">后台数据报错:'+data.errorMsg+'！</span>';
							exportBtnHtml += '</div>';
							$("#"+tabId+" #generateBtn").removeAttr('disabled', 'disabled').find('i').removeClass('fa-spin');
							$('#exportResult .panel-footer').html(exportBtnHtml);
							drgUI.error('后台数据报错:'+data.errorMsg);
						}
					}
				}
			});
});
// 生成分析报告所需图表
function createPicture(data) {
	 iscreate=0;
	console_wt('开始生成图表...');
	console_wt('构建手术与非手术图...');
	var chartData = [ {
		name : '手术',
		value : data.caseOperCount
	}, {
		name : '非手术',
		value : data.hospoutcasecnt - data.caseOperCount
	} ]
 reportPieChart('isopername', $('#image1'), chartData);
	console_wt('构建手术级别分布图...');
	chartData = [];
	 if(data.level1!=0){
	    chartData.push({name : '一级',value : data.level1})
	   }
	 if(data.level2!=0){
		    chartData.push({name : '二级',value : data.level2})
		   }
	 if(data.level3!=0){
		    chartData.push({name : '三级',value : data.level3})
		   }
	 if(data.level4!=0){
		    chartData.push({name : '四级',value : data.level4})
		   }
	 if(data.leveloth!=0){
		    chartData.push({name : '其他',value : data.leveloth})
		   }
	reportPieChart('operlevelname', $('#image2'), chartData);
	console_wt('构建排名前二十名的手术级别分布图...');
	chartData = [];
	var leveldata = data.mainOperTop20List;
	for (var i = 0; i < leveldata.length; i++) {
		chartData.push({
			name : leveldata[i].level,
			value : leveldata[i].cnt
		})
	}
	reportPieChart('mainOperTop20Ration', $('#image5'), chartData);
	
	chartData = [];
	console_wt('构建DRG病组类型分布图...');
	 if(data.indrgcnt!=0){
		    chartData.push({name : '内科组',value : data.indrgcnt})
		   }
	 if(data.outdrgcnt!=0){
		    chartData.push({name : '外科组',value : data.outdrgcnt})
		   }
	 if(data.operdrgcnt!=0){
		    chartData.push({name : '操作组',value : data.operdrgcnt})
		   }
	reportPieChart('drgTypeCntRation', $('#image3'), chartData);

	console_wt('构建DRG病例类型分布图...');
	chartData = [];
	 if(data.incasecnt!=0){
		    chartData.push({name : '内科组',value : data.incasecnt})
		   }
	 if(data.outcasecnt!=0){
		    chartData.push({name : '外科组',value : data.outcasecnt})
		   }
	 if(data.opercasecnt!=0){
		    chartData.push({name : '操作组',value : data.opercasecnt})
		   }

	reportPieChart('caseTypeCntRation', $('#image4'), chartData);

	console_wt('构建DRG病组复杂程度分布图...');
	chartData = [];
	 if(data.DRGsType1Num!=0){
		    chartData.push({name : '伴重要合并症和伴随病',value : data.DRGsType1Num})
		   }
	 if(data.DRGsType3Num!=0){
		    chartData.push({name : '伴合并症和伴随病',value : data.DRGsType3Num})
		   }
	 if(data.DRGsType5Num!=0){
		    chartData.push({name : '不伴合并症和伴随病',value : data.DRGsType5Num})
		   }
	 if(data.DRGsType7Num!=0){
		    chartData.push({name : '死亡或转归',value : data.DRGsType7Num})
		   }
	 if(data.DRGsType9Num!=0){
		    chartData.push({name : '其他',value : data.DRGsType9Num})
		   }
	reportPieChart('drgCntRation', $('#image7'), chartData);

	console_wt('构建DRG病例复杂程度分布图...');
	chartData = [];
	 if(data.CaseType1Num!=0){
		    chartData.push({name : '伴重要合并症和伴随病',value : data.CaseType1Num})
		   }
	 if(data.CaseType3Num!=0){
		    chartData.push({name : '伴合并症和伴随病',value : data.CaseType3Num})
		   }
	 if(data.CaseType5Num!=0){
		    chartData.push({name : '不伴合并症和伴随病',value : data.CaseType5Num})
		   }
	 if(data.CaseType7Num!=0){
		    chartData.push({name : '死亡或转归',value : data.CaseType7Num})
		   }
	 if(data.CaseType9Num!=0){
		    chartData.push({name : '其他',value : data.CaseType9Num})
		   }
	
	reportPieChart('caseCntRation', $('#image8'), chartData);
	console_wt('构建科室（CMI和总权重）...');
	chartData = [];

	for (var i = 0; i < data.depnormalWeightAndCMIList.length; i++) {
		chartData.push({
			name : '',
			value : [ data.depnormalWeightAndCMIList[i].cmi,
					data.depnormalWeightAndCMIList[i].weight ]
		});
	}
	reportScatterChart('cmiandweightScanter', $('#image6'), "CMI", "总权重",chartData, data.depmiddleWeight,data.depmiddleCmi);
	console_wt('构建各临床科室费用和时间消耗指数象限分布图...');
	chartData = [];
	for (var i = 0; i < data.depFeeAndTimeEffMapList.length; i++) {
		chartData.push({
			name : '',
			value : [ data.depFeeAndTimeEffMapList[i].timeEff,
					data.depFeeAndTimeEffMapList[i].feeEff ]
		});
	}
	reportScatterChart('imgDepFeeTime', $('#image9'), "时间消耗指数", "费用消耗指数",chartData, 1,1);
	
	console_wt('构建重点科室mdc费用和时间消耗指数象限分布图...');
	chartData = [];
	for (var i = 0; i < data.findtable63mapList.length; i++) {
		chartData.push({
			name : '',
			value : [ data.findtable63mapList[i].table63_time,
					data.findtable63mapList[i].table63_fee ]
		});
	}
	reportScatterChart('imgDepFeeTime14', $('#image14'), "时间消耗指数", "费用消耗指数",chartData, 1,1);
	
	console_wt('构建重点科室DRG费用和时间消耗指数象限分布图...');
	chartData = [];
	for (var i = 0; i < data.findchart15mapList.length; i++) {
		chartData.push({
			name : '',
			value : [ data.findchart15mapList[i].chart15_time,
					data.findchart15mapList[i].chart15_fee ]
		});
	}
	reportScatterChart('imgDepFeeTime15', $('#image15'), "时间消耗指数", "费用消耗指数",chartData, 1,1);
	
	
	console_wt('构建MDC专业象限分布图（CMI和总权重）...');
	chartData = [];
	for (var i = 0; i < data.mdcnormalCWIMapList.length; i++) {
		chartData.push({
			name : '',
			value : [ data.mdcnormalCWIMapList[i].cmi,
					data.mdcnormalCWIMapList[i].weight ]
		});
	}
	reportScatterChart('mdcCmiWeightRtio', $('#image10'), "CMI", "总权重",chartData, data.mdcmiddleWeight,data.mdcmiddleCmi);

    console_wt('构建MDC专业象限分布图（费用消耗指数和时间消耗指数）...');
	chartData = [];
	for (var i = 0; i < data.mdcFTMapList.length; i++) {
		chartData.push({
			name : '',
			value : [ data.mdcFTMapList[i].time,
					data.mdcFTMapList[i].fee ]
		});
	}
	reportScatterChart('mdcFeeTimeRtio', $('#image11'), "时间消耗指数", "费用消耗指数",chartData, 1,1);
	console_wt('构建未入组原因饼图');
	chartData = [];
	 if(data.reason1!=0){
		    chartData.push({name : '手术/操作与诊断不符',value : data.reason1})
		   }
	 if(data.reason2!=0){
		    chartData.push({name : '费用/住院日异常',value : data.reason2})
		   }
	 if(data.reason3!=0){
		    chartData.push({name : '缺少主要诊断',value : data.reason3})
		   }
	 if(data.reason4!=0){
		    chartData.push({name : '主要诊断有误',value : data.reason4})
		   }
	 if(data.reason5=0){
		    chartData.push({name : '新生儿年龄/体重有误',value : data.reason5})
		   }
	 if(data.reason6!=0){
		    chartData.push({name : '其他',value : data.reason6})
		   }	
	reportPieChart('notInGroupRatio', $('#image12'), chartData);
	
  /*  console_wt('构建MDC专业象限分布图（费用消耗指数和时间消耗指数）...');
	chartData = [];
	for (var i = 0; i < data.mdcFeeAndTimeEffMapList.length; i++) {
		chartData.push({
			name : '',
			value : [ data.mdcFeeAndTimeEffMapList[i].time,
					data.mdcFeeAndTimeEffMapList[i].fee ]
		});
	}
	reportScatterChart('mdcFeeTimeRtio', $('#image11'), "时间消耗指数", "费用消耗指数",chartData, 1);*/
	iscreate=1;
	console_wt('图表构建完毕！');
	return true;
}
function console_wt(s) {
	$('#exportResult .panel-footer').append('<p>' + s + '</p>');
}